/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Models.DAOs;

import DBConnection.ConnectionFactory;
import Models.DVDs;
import Security.Encrypter;
import com.mysql.jdbc.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import Security.Master_check;

/**
 *
 * @author 0905
 */
public class DVD_DAO extends Product_DAO{
    public boolean addDVD(DVDs dvd){
        try{
            Master_check checker = new Master_check();
            if(checker.dvdcheck(dvd)){
                int productID = this.addProduct(dvd);
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                int i = 1;

                PreparedStatement pstmtDVD = conn.prepareStatement("INSERT INTO dvds(releaseDate, genre, numDiscs, productID) VALUES(?,?,?,?)");
                pstmtDVD.setString(i++, dvd.getReleaseDate());
                pstmtDVD.setString(i++, dvd.getGenre());
                pstmtDVD.setInt(i++, dvd.getNumDiscs());
                pstmtDVD.setInt(i++,productID ); 

                pstmtDVD.executeUpdate();
                return true;
            }
            else
                return false;
        }
        
        catch(SQLException e){
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean updateDVD(DVDs dvd, String dvdCode){
        try{
            Master_check checker = new Master_check();
            if(checker.dvdcheck(dvd)){
                String encryptedDVD = new Encrypter().decrypt(dvdCode);
                dvd.setProductID(Integer.parseInt(encryptedDVD));

                boolean updatedProduct = this.updateProduct(dvd);

                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                int i = 1;

                PreparedStatement pstmtDVD = conn.prepareStatement("UPDATE dvds SET releaseDate = ?, genre = ?, numDiscs = ? WHERE productID = ?");          
                pstmtDVD.setString(i++, dvd.getReleaseDate());
                pstmtDVD.setString(i++, dvd.getGenre());
                pstmtDVD.setInt(i++, dvd.getNumDiscs());
                pstmtDVD.setInt(i++, dvd.getProductID());
                pstmtDVD.executeUpdate();
                return true;
            }
            else
                return false;
        }
        catch(Exception e){
            
            return false;
        }
    }
    
    
     public ArrayList<DVDs> getAllDVDs(){
        
        DVDs dvd;
        ArrayList<DVDs> dvdList = null;
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            CallableStatement pstmt = (CallableStatement) conn.prepareCall("{ call GET_ALL_DVDS() }");
            ResultSet rs = pstmt.executeQuery();
            dvdList = new ArrayList<DVDs>();
            Encrypter encrypt = new Encrypter();
            while(rs.next())
            {
                dvd = new DVDs();
                
                //System.out.println(encrypt.encrypt());
                dvd.setProductCrypt(encrypt.encrypt(Integer.toString(rs.getInt("productID"))));
                //String str  = Integer.toString(rs.getInt("prodcutID"));
                //System.out.println(str);
                dvd.setProductID(rs.getInt("productID"));               
                dvd.setDvdID(rs.getInt("dvdID"));
                dvd.setGenre(rs.getString("genre"));
                dvd.setReleaseDate(rs.getString("releaseDate"));
                dvd.setTitle(rs.getString("title"));
                //dvd.setAuthor(rs.getString("author"));
                dvd.setPrice(rs.getFloat("price"));
                dvd.setSynopsis(rs.getString("synopsis"));
                dvd.setNumDiscs(rs.getInt("numDiscs"));
                
               
                dvdList.add(dvd);
            }
            
            return dvdList;
        }
        catch(SQLException e){
        
            return dvdList;
        }
            
    }
     
     public DVDs getDVD(int dvdID){
        
        DVDs dvd = null;
        
        try{
        
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dvds b INNER JOIN products p ON b.productID = p.productID WHERE b.productID = ?");
            pstmt.setInt(1, dvdID);
            
            ResultSet rs = pstmt.executeQuery();
            if(rs.next())
            {
                dvd = new DVDs();
                dvd.setProductCrypt(new Encrypter().encrypt(Integer.toString(rs.getInt("productID"))));
                dvd.setDvdID(rs.getInt("dvdID"));
                dvd.setGenre(rs.getString("genre"));
                dvd.setReleaseDate(rs.getString("releaseDate"));
                dvd.setTitle(rs.getString("title"));
                //dvd.setAuthor(rs.getString("author"));
                dvd.setPrice(rs.getFloat("price"));
                dvd.setSynopsis(rs.getString("synopsis"));
                dvd.setNumDiscs(rs.getInt("numDiscs"));               
            }
            return dvd;           
        }
        catch(SQLException e){
        
            e.printStackTrace();
            return dvd;
        }    
    }
    
    
}
